package com.nervenets.web.controller.handler.user.wealth;

import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.UserPagingParams;
import com.nervenets.web.enumeration.OperateType;
import com.nervenets.web.enumeration.WealthType;
import com.nervenets.web.hibernate.domain.user.User;
import com.nervenets.web.redis.domain.user.WealthOfMoneyByOperateType;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class UserInvitesHandler extends Handler<UserPagingParams> {
    @Override
    public String handle(UserPagingParams params, HttpServletRequest request) throws Exception {
//        String hql = "from User where inviteUserId = ? or teamUserId = ? ";
        String hql = "from User where inviteUserId = ? ";//暂时只显示一级的数据
        List<Object> objects = new ArrayList<>();
        objects.add(params.getUserId());
//        objects.add(params.getUserId());

        BigDecimal countOfByHql = userDao.getCountOfByHql(hql, objects);
        hql += "order by id desc ";
        List<User> inviteUsers = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());

        WealthOfMoneyByOperateType totalThanks = globalService.getUserWealthOfMoneyByOperateType(params.getUserId(), OperateType.dividends, WealthType.reward);
        return successMessage(new Model(totalThanks.getTotal(), countOfByHql.longValue(), inviteUsers));
    }

    @Override
    public String result() {
        return null;
    }

    class Model {
        long totalThanks;
        long total;
        List<User> inviteUsers;

        public Model(long totalThanks, long total, List<User> inviteUsers) {
            this.totalThanks = totalThanks;
            this.total = total;
            this.inviteUsers = inviteUsers;
        }
    }
}
